<template>
  <div>
    <div class="box"></div>
    <div class="loginBox">
      <p class="title">用户注册</p>
      <el-form :model="user" status-icon :rules="rules" ref="user" label-width="70px" class="demo-ruleForm">
        <el-form-item label="用户名" prop="username" class="item">
          <el-input size="mini" type="text" v-model="user.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="昵称" prop="nickname" class="item">
          <el-input size="mini" type="text" v-model="user.nickname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="phone" class="item">
          <el-input size="mini" type="text" v-model="user.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password" class="item">
          <el-input size="mini" type="password" v-model="user.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkpwd" class="item">
          <el-input size="mini" type="text" v-model="user.checkpwd" autocomplete="off"></el-input>
        </el-form-item>
      <el-button class="btn" @click="addUser()">注册</el-button>
      </el-form>
      <div class="fot">
         <p>已有账号?<button @click="toLogin">立即登录</button></p>
      </div>
    </div>
  </div>
</template>

<script>
// import { addUser } from '../../api/api'
export default {
  data () {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        const reg = /^(?![0-9]+$)(?![a-z]+$)(?![A-Z]+$)(?!([^(0-9a-zA-Z)])+$).{6,15}$/
        if (!reg.test(value)) {
          callback(new Error('密码包含 数字,英文,字符中的两种以上，长度6-15'))
        }
      }
    }
    var validatePhone = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入手机号'))
      } else {
        const reg = /^1[3578]\d{9}$/
        if (!reg.test(value)) {
          callback(new Error('手机号输入错误1'))
        }
      }
    }
    var validateEmail = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入'))
      } else {
        const reg = /^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-_.])+[A-Za-z\d]{2,4}$/
        if (!reg.test(value)) {
          callback(new Error('请输入正确的邮箱'))
        }
      }
    }
    var validate = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入'))
      }
    }
    return {
      login: 'login',
      user: {
        username: '',
        phone: '',
        checkpwd: '',
        password: '',
        nickname: ''
      },
      rules: {
        phone: [
          { validator: validatePhone, trigger: 'blur' }
        ],
        account: [
          { validator: validate },
          { min: 3, max: 10, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        pass: [
          { validator: validatePass, trigger: 'blur' }
        ],
        email: [
          { validator: validateEmail, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    toLogin () {
      this.$bus.$emit('title', this.login)
    },
    addUser () {
      this.$axios.post('/register', this.user).then((res) => {
        console.log(res)
        if (res.data.code === 200) {
          this.toLogin()
        }
      }).catch(err => {
        console.error(err)
      })
      // addUser(this.user).then((request) => {
      //   console.log(request)
      // }).catch(err => {
      //   console.error(err)
      // })
    }
  }
}
</script>
<style lang="less" scoped>
.box{
  width: 300px;
  height: 400px;
  background-image: url('https://img0.baidu.com/it/u=2042637590,3523032130&fm=253&fmt=auto?w=1200&h=800');
  background-size: 240%;
  background-position: -200px;
  background-repeat: no-repeat;
  display: inline-block;
}
.loginBox{
  width: 260px;
  height: 400px;
  overflow: hidden;
  padding: 20px;
  display:inline-block;
  background-color: rgb(242, 242, 243);
  p{
    font-size: 10px;
  }
  .title{
    padding: 20px;
    padding-left: 40px;
    font-size: 39px;
    margin-bottom: 15px;
    // margin-top: 10px;
  }
  .btn{
    position: relative;
    top: 10px;
    left: 50%;
    transform: translateX(-40%);
    height: 20px;
    line-height: 5px;
  }
  .item{
    margin-top: 5px;
    height: 30px;
    input{
      height: 20px;
    }
  }
  .fot{
    // margin-top: 10px;
    margin-left: 100px;
    width: 200px;
  }
}
button{
  border: 0px;
}
</style>
